//
//  ScanListTVC.m
//  POS
//
//  Created by sigma xu on 4/30/13.
//  Copyright (c) 2013 sigma xu. All rights reserved.
//

#import "ItemListTVC.h"


@interface ItemListTVC ()

@end

@implementation ItemListTVC
@synthesize item_list;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section ==0) {
        NSString *result=[[NSString alloc]initWithFormat:@"Total Item: %d",item_list.count ];
        return result;
    }
    return @"";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return item_list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ItemListCell1";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil){
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleValue1
                reuseIdentifier:CellIdentifier];
    }
    if (indexPath.section == 0) {
        ItemDBO *idbo = [[ItemDBO alloc] init];
        idbo = [item_list objectAtIndex:indexPath.row];
        cell.textLabel.text = idbo.item_name;
//        NSLog(@"cell %@",idbo.item_barcode);
        NSString *detail_text = @"$ ";
        detail_text= [detail_text stringByAppendingString:idbo.unit_price];
        detail_text= [detail_text stringByAppendingString:@" X "];
        detail_text= [detail_text stringByAppendingString:idbo.quantity];
        detail_text= [detail_text stringByAppendingString:@" "];
        detail_text= [detail_text stringByAppendingString:idbo.unit];
        cell.detailTextLabel.text = detail_text;
    }
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        ScanVC *svc = (ScanVC *)[self.navigationController.viewControllers objectAtIndex:0];
//        [item_list removeObjectAtIndex:indexPath.row];
        [svc.sv_item_list removeObjectAtIndex:indexPath.row];
        [svc calculate_total_amount];
        self.title = [[NSString alloc]initWithFormat:@"$ %.2f",svc.total_amount];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
